/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-05
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-02-08
 * @Description: 
 */
import 'package:chat_client/repositories/auth_repository.dart';
import 'package:flutter/material.dart';
import 'package:chat_client/widgets/global_widgets.dart'; // 引入公共的 组件
import 'package:chat_client/services/auth_service.dart';
import 'package:chat_client/services/user_service.dart';

import 'package:chat_client/config/api_config.dart';

class ProfileScreen extends StatefulWidget {
  const ProfileScreen({super.key});

  @override
  ProfileScreenState createState() => ProfileScreenState();
}

class ProfileScreenState extends State<ProfileScreen> {
  final AuthService authService = AuthService.instance;
  final AuthRepository authRepository = AuthRepository.instance;
  final UserService userService = UserService.instance;

  // 个人信息
  Map<String, dynamic>? user;

  @override
  void initState() {
    super.initState();
    // 在生命周期初始化时加载用户信息
    _loadUserInfo();
  }

  // 加载用户信息
  Future<void> _loadUserInfo() async {
    user = await userService.getLocalUserInfo(); // 异步加载用户信息
    setState(() {}); // 更新 UI
  }

  Future<void> _register() async {
    // 注册逻辑
  }

  @override
  Widget build(BuildContext context) {
    // 如果用户信息还没加载完成，显示加载中
    if (user == null) {
      return const Center(child: CircularProgressIndicator());
    } else {
      return Column(
        children: [
          customUserInfo(
            title: user?['nickname'],
            subtitle: '账号: ${user!['phoneNumber']}',
            imageUrl: baseUrl + user!['avatarUrl'],
            size: 80,
            trailing: const Icon(Icons.qr_code),
          ),
          const DividerWithText(),
          customListFunction(
            title: '个人资料',
            icon: Icons.badge,
            iconColor: Colors.blue[500],
          ),
          buildDivider(),
          customListFunction(
            title: '朋友圈',
            icon: Icons.broken_image,
            iconColor: Colors.blue[500],
          ),
          const DividerWithText(),
          customListFunction(
            title: '设置',
            icon: Icons.settings,
            iconColor: Colors.blue[500],
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            onPressed: _register,
            child: const Text('注册'),
          ),
        ],
      );
    }
  }
}
